package com.osdp.uc.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.osdp.common.util.BeansUtil;
import com.osdp.uc.dto.OrganizationDTO;
import com.osdp.uc.dto.PositionDTO;
import com.osdp.uc.dto.UserDTO;
import com.osdp.uc.service.IUcOrganizationService;
import com.osdp.uc.service.IUcPositionService;
import com.osdp.uc.service.IUcUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/rest")
@Api(tags = "Feign接口管理")
@Slf4j
public class UcServiceRest {

    @Autowired
    private IUcUserService ucUserService;

    @Autowired
    private IUcOrganizationService organizationService;

    @Autowired
    private IUcPositionService positionService;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;
//
//    @GetMapping("/index")
//    public String getIndex(){
//        ServiceInstance serviceInstance = this.loadBalancerClient.choose("micro-auth");
//        log.info("{}:{}:{}", serviceInstance.getServiceId(),
//                serviceInstance.getHost(),serviceInstance.getPort());
//        System.out.println("ServiceId: "+ serviceInstance.getServiceId()+
//                "----------host: "+serviceInstance.getHost()+"-------------port: "+serviceInstance.getPort());
//        return "hello index";
//    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/user/byUsername")
    public UserDTO queryUserByUserName(@RequestParam(value = "userName") String userName){
        return ucUserService.queryUserByUserName(userName);
    }

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/user/byId")
    public UserDTO queryUserById(@RequestParam(value = "userId") String userId){
        return ucUserService.queryUserById(userId);
    }

    @ApiOperation(value = "查询所有用户列表（分页）", notes = "查询所有用户列表（分页）")
    @PostMapping("/user/pageList")
    public IPage<UserDTO> userPageList(@RequestBody UserDTO userDTO,
            @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") int pageSize){
        return ucUserService.userPageList(userDTO,new Page<>(pageNo, pageSize)).convert(track -> BeansUtil.copy(track, new UserDTO()));
    }

    @ApiOperation(value = "根据用户ID修改密码", notes = "修改密码")
    @GetMapping("/user/updatePassWord")
    public Boolean updatePassWord(@RequestParam(value = "id")  String id,
                                          @RequestParam(value = "passWord") String passWord){
        return ucUserService.updatePassWord(id,passWord);
    }

    /**
     * 根据用户ID对应的岗位信息
     * @param userId
     * @return List<PositionVo>
     */
    @ApiOperation(value = "根据用户ID获取岗位信息", notes = "获取岗位信息")
    @GetMapping("/position/positionByUserId")
    public List<PositionDTO> queryPositionByUserId(@RequestParam(value = "userId") String userId){
        return positionService.queryPositionByUserId(userId);
    }

    /**
     * 根据用户ID得到相应的组织机构
     * @param userId
     * @return List<OrganizationVo>
     */
    @ApiOperation(value = "根据用户ID获取部门信息", notes = "获取部门信息")
    @GetMapping("/org/orgByUserId")
    public List<OrganizationDTO> queryOrganizationByUserId(@RequestParam(value = "userId") String userId){
        return organizationService.queryOrganizationByUserId(userId);
    }
}
